package com.mes.check.service;


import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.mes.check.dao.iface.ICheckTaskDao;
import com.mes.check.domain.CheckTask;
import com.mes.check.domain.CheckTaskAssign;
import com.mes.check.manager.iface.ICheckTaskManager;
import com.mes.common.service.BasicService;
import com.mes.common.util.SysUtil;
import com.mes.task.dao.iface.IOperationTaskDao;
import com.mes.task.dao.iface.IPartTaskDao;

@Service
public class CheckTaskService extends BasicService<CheckTask> {

	@Autowired
	SysUtil sysUtil;
	@Autowired
	IOperationTaskDao operationTaskDao;
	@Autowired
	IPartTaskDao partTaskDao;
	@Autowired
	ICheckTaskManager checkTaskManager;
	
	@Autowired
	ICheckTaskDao checkTaskDao;
	
	/**
	 * 获取工序任务的流动检验任务
	 * @param taskUid
	 * @return
	 * @throws Exception
	 */
	@DataProvider
	public CheckTask getOperationCheckTask(String taskUid)
			throws Exception {
		return checkTaskDao.getOperationCheckTask(taskUid);
	}
	
	/**
	 * 获取物料已经就绪的检验任务
	 * @param page
	 * @param parameter
	 * @param criteria
	 * @throws Exception
	 */
	@DataProvider
	public void getMaterialReadyCheckTasks(Page<CheckTask> page, Map<String, Object> parameter,
			Criteria criteria) throws Exception {
		checkTaskDao.getMaterialReadyCheckTasks(page, parameter, criteria);
	}
	
	/**
	 * 获取收料数量
	 * @param taskUid
	 * @param checkDeptUid
	 * @return
	 * @throws Exception
	 */
	@DataProvider
	public Float getReceivedQty(String taskUid, String checkDeptUid) throws Exception {
		return checkTaskDao.getReceivedQty(taskUid, checkDeptUid);
	}
	
	/**
	 * 获取收料数量
	 * @param checkTask
	 * @return
	 * @throws Exception
	 */
	@DataProvider
	public Float getReceivedQty(CheckTask checkTask) throws Exception {
		return checkTaskDao.getReceivedQty(checkTask);
	}
	
	/**
	 * 获取派工数量
	 * @param checkTaskUid
	 * @return
	 * @throws Exception
	 */
	@DataProvider
	public Float getDispatchedQty(String checkTaskUid) throws Exception {
		return checkTaskDao.getDispatchedQty(checkTaskUid);
	}
	
	/**
	 * 检验任务派工
	 * @param dataItems
	 * @param parameter
	 * @throws Exception
	 */
	@DataResolver
	public void dispatchCheckTasks(Map<String, Collection<CheckTask>> dataItems, Map<String, String> parameter) throws Exception {
		String checkerUid = parameter.get("checkerUid");
		Collection<CheckTask> checkTasks = dataItems.get("dsCheckTask");
		for(CheckTask checkTask : checkTasks){
			checkTaskManager.dispatchCheckTask(checkTask, checkerUid, null);
		}
	}
	
	/**
	 * 取消检验任务派工
	 * @param dataItems
	 * @throws Exception
	 */
	@DataResolver
	public void cancelDispatchCheckTasks(Map<String, Collection<CheckTaskAssign>> dataItems) throws Exception {
		Collection<CheckTaskAssign> checkTaskAssns = dataItems.get("dsCheckTaskAssign");
		for(CheckTaskAssign assn : checkTaskAssns){
			checkTaskManager.cancelDispatchCheckTask(assn);
		}
	}
	
	/**
	 * 开始检验任务
	 * @param checkTaskUid
	 * @throws Exception
	 */
	@Expose
	public void startCheckTask(String checkTaskUid) throws Exception {
		checkTaskManager.startCheckTask(checkTaskUid);
	}
	
	/**
	 * 完成检验任务
	 * @param checkTaskAssnUid
	 * @throws Exception
	 */
	@Expose
	public void completeCheckTask(String checkTaskAssnUid) throws Exception {
		checkTaskManager.feedbackCheckTask(checkTaskAssnUid);
	}
}
